#
#           _     _                 _
#  ___ _ __(_)___| |__   ___   ___ | |_
# / __| '__| / __| '_ \ / _ \ / _ \| __|
#| (__| |  | \__ \ |_) | (_) | (_) | |_
# \___|_|  |_|___/_.__/ \___/ \___/ \__|
#                automation honor!!!!!!
#
#
# Complete Functional GUI Automated Test
# 
# Author: Cristian Cortez
# Date: Thursday, January 5, 2012 
# License: WTFPL
# Platform: Windows 64-bits
# Dependencies: Ruby, Gem, Chrome Webdriver
# Gems List: watir, watir-webdriver
#
# Disclaimer:
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#

# OS	Expected Location of Chrome
# Linux	 /usr/bin/google-chrome
# Mac	 /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
# Windows XP	 %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
# Windows Vista	 C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe


#settings
$host = "http://dev.nodoinmobiliario.com/"

$cred = {
    "email" => "demo@demo.com",
    "pwd" => "12345678" 
}

$browserUTAddTime = {
	"ff" => 0,
	"chrome" => 0,
	"ie" => 3,
	"ieChangeMode" => 8,
	"ie9" => "IE9",
	"ie8" => "IE8",
	"ie7" => "IE7"
}

# Selecting browser
puts "Please select a Browser (ff, ie, chrome):"
$browserSelect = STDIN.gets.split("\n")[0].to_s
# FPO -
# $browserSelect = "chrome"
puts "[info] Starting tests with "+$browserSelect

if $browserSelect == 'ie' then
	require 'watir'
	$browser = Watir::Browser.new
	puts "[info] Now you have " + $browserUTAddTime["ieChangeMode"].to_s + " seconds to change the mode"
	sleep $browserUTAddTime[$browserSelect] + $browserUTAddTime["ieChangeMode"]
else
	require 'watir-webdriver'
	
	if $browserSelect == 'chrome' then
		system('start /d "C:\\Users\\%USERNAME%\\AppData\\Local\\Google\\Chrome\\Application\\" chromedriver.exe')
		$browser = Watir::Browser.new(:remote, :url => "http://localhost:9515")
	else
		$browser = Watir::Browser.new($browserSelect)
	end
end



def go webPage
    $browser.goto webPage
end

def testHomePage
    go $host
end

def testLogin
    go $host
    $browser.text_field(:id, "email").set($cred["email"])
    $browser.text_field(:id, "pwd").set($cred["pwd"])
#    $browser.hidden(:name, "csrf").set("xxxx")
    $browser.button(:id, "signin").click
end

def closeBrowser
    puts "press a key to close the browser"
    STDIN.gets
    $browser.close
end

#Tests to execute
#testHomePage
testLogin
closeBrowser